package com.future.mayi.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.activity.GoodsOrderActivity;
import com.future.mayi.model.WalletInModel;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;


public class WalletInAdapter extends BaseRecyclerViewAdapter<WalletInModel> implements View.OnClickListener {

    private int type;


    public WalletInAdapter(Context context, XRecyclerView xRecyclerView, int type) {
        super(context, null, xRecyclerView);
        this.type = type;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(getView(parent, R.layout.layout_wallet_item));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (!(viewHolder instanceof ItemViewHolder)) return;
        ItemViewHolder holder = (ItemViewHolder) viewHolder;

        WalletInModel model = getItem(position);
//        if (model.getType() == 0) {
//            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.text_color26AA51));
//        } else {
//            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.text_colorFF8D6C));
//        }

        holder.tvTitle.setText(model.getName() == null ? "" : model.getName());
        setText(holder.tvTime, model.getCreatetime());
        holder.tvMoney.setText((model.getType() == 0 ? "+" : "-") + "¥" + model.getPrice());
        if (model.getOrder_no() != null) {
            holder.tvOrder.setVisibility(View.VISIBLE);
            holder.tvOrder.setText("(订单号：" + model.getOrder_no() + ")");
        } else
            holder.tvOrder.setVisibility(View.GONE);


        if (model.getChange_type() == 10) {
            holder.tvReason.setVisibility(View.VISIBLE);
            String text = model.getCash_info().getStatus() == 1 ? "提现成功" : (model.getCash_info().getStatus() == 10 ? "提现失败" : "待审核");
            if (!TextUtils.isEmpty(model.getCash_info().getRefusal_reason()))
                text = text + "（" + model.getCash_info().getRefusal_reason() + "）";
            setText(holder.tvReason, text);
        } else if (!TextUtils.isEmpty(model.getRemark())) {
            holder.tvReason.setVisibility(View.VISIBLE);
            setText(holder.tvReason, model.getRemark());
        } else holder.tvReason.setVisibility(View.GONE);


        Drawable drawable = null;
        if (model.getChange_type() != 11 && (model.getRebate_type() == 0 || model.getRebate_type() == 1)) {
            drawable = mContext.getResources().getDrawable(R.drawable.img_in_s);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        holder.tvOrder.setCompoundDrawables(null, null, drawable, null);
        holder.tvOrder.setTag(position);
        holder.tvOrder.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getTag() == null) return;
        if (!(v.getTag() instanceof Integer)) return;
        int position = (int) v.getTag();
        WalletInModel model = getItem(position);
        if (model.getOrder_id() == 0) return;
        if (model.getChange_type() != 11 && (model.getRebate_type() == 0 || model.getRebate_type() == 1))
            GoodsOrderActivity.startActivity(mContext, model.getRebate_type() == 1, String.valueOf(model.getOrder_id()));
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_order)
        TextView tvOrder;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_reason)
        TextView tvReason;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
